package com.lego.jbv;
import com.lego.jbv.DataLogger;

import lejos.nxt.*;
/**
 * A simple sonar sensor test program.
 * 
 * The sensor should be connected to port 1. In the
 * known bugs and limitations of leJOS NXJ version alfa_03
 * it is mentioned that a gap of at least 300 msec is 
 * needed between calls of getDistance. This is the reason 
 * for the delay of 300 msec between sonar readings in the 
 * loop.
 * @version 30.08.07
 */
public class SonicSensorTest 
{

   public static void main(String [] args)  
   throws Exception 
   {
	int measureInterval = 120;
       UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);

       LCD.drawString("Distance(cm) ", 0, 0);
       
       //The DataLogger is only used for exercise 2
       //DataLogger data = new DataLogger("distances" + Integer.toString(measureInterval) + ".csv");
       //data.start();
	   
       int distance = -1;
       
       /*data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);
       data.writeSample(distance);*/
       
       while (!Button.ESCAPE.isDown())
       {
    	   distance = us.getDistance();
           LCD.drawInt(distance,3,13,0);
           //data.writeSample(distance);
           Thread.sleep(measureInterval);
       }
       LCD.clear();
       LCD.drawString("Program stopped", 0, 0);
       //data.close();
       Thread.sleep(2000);
   }
}

